package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbBrandMapper;
import com.dongdongshop.page.PageResult;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.pojo.TbBrandExample;
import com.dongdongshop.service.TbBrandService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
@DubboService
public class TbBrandServiceImpl implements TbBrandService {
    @Autowired
    private TbBrandMapper brandMapper;

    @Override
    public void addBrand(TbBrand brand) {
        brandMapper.insertSelective(brand);
    }

    @Override
    public TbBrand selectBrandById(Integer id) {
        TbBrand brand = brandMapper.selectByPrimaryKey(Long.valueOf(id));
        return brand;
    }

    @Override
    public void updateBrandById(TbBrand brand) {
        brandMapper.updateByPrimaryKey(brand);
    }

    @Override
    public PageResult listBrands(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbBrand> tbBrands = brandMapper.selectByExample(null);
        PageInfo<TbBrand> in = new PageInfo<>(tbBrands);
        return new PageResult(in.getPageNum(),in.getPageSize(),in.getTotal(),in.getPages(),in.getList());
    }

    @Override
    public void deleteBrands(Long[] id) {
        List<Long> ids = Arrays.asList(id);
        TbBrandExample example = new TbBrandExample();
        TbBrandExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(ids);
        int i = brandMapper.deleteByExample(example);
        System.out.println(i);
    }

    @Override
    public List<TbBrand> listBrandsAll() {
        return brandMapper.selectByExample(null);
    }

    @Override
    public void deleteBrand(Integer id) {
        brandMapper.deleteByPrimaryKey(Long.valueOf(id));
    }
}
